package com.tz.crm.utils;

import org.apache.log4j.Logger;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 用来验证网址是否可以正常访问
 *
 * @author 陈成
 * @since 2016/12/19
 */
public final class WebSiteCheckUtil {

    private static final Logger LOGGER = Logger.getLogger(WebSiteCheckUtil.class);

    /**
     * 空参构造
     */
    private WebSiteCheckUtil() {
    }

    /**
     * 校验网址有效性
     *
     * @param urlStr url String
     * @return 可用返回true, 反之false
     */
    public static boolean validateWebUrl(String urlStr) {
        boolean flag = false;
        try {
            URL url = new URL(urlStr);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setConnectTimeout(400);
            int state = con.getResponseCode();
            if (state == 200) {
                flag = true;
            }
        } catch (IOException e) {
            LOGGER.debug("网址无效，请核对网址是否正确");
            e.printStackTrace();
            return flag;
        }
        return flag;
    }
}
